package org.ims.inv.service.system.impl;

import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.ims.inv.dao.system.DAOAppProjectGroup;
import org.ims.inv.model.system.ModelAppProjectGroup;
import org.ims.inv.service.system.ServiceAppProjectGroup;

import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;

/**
 * Service: 系统项目组
 * 
 * @author Jeccy
 *
 */
public class ServiceAppProjectGroupImpl
extends ServiceGenericImpl<ModelAppProjectGroup> implements ServiceAppProjectGroup
{
	
	private DAOAppProjectGroup daoAppProjectGroup;
	
	/**
	 * The sole constructor
	 * 
	 * @param dao
	 */
	public ServiceAppProjectGroupImpl(DAOAppProjectGroup dao)
	{
		super(dao);
		this.daoAppProjectGroup = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelAppProjectGroup domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAppProjectGroup.class);
		
		if (domain != null)
		{
			if (UtilString.isNotEmpty(domain.getGroupName()))
			{
				criteria.add(Restrictions.like("groupName", domain.getGroupName(), MatchMode.ANYWHERE));
			}
			
			if (UtilString.isNotEmpty(domain.getGroupLeaderIds(), domain.getGroupMemberIds()))
			{
				criteria.add(Restrictions.sqlRestriction(
						"FIND_IN_SET(?, `group_leader_ids`) > 0 or FIND_IN_SET(?, `group_members_ids`) > 0", 
						new Object[] { domain.getGroupLeaders(), domain.getGroupMembers()}, 
						new Type[] {Hibernate.STRING, Hibernate.STRING}));
			}
			else if (UtilString.isNotEmpty(domain.getGroupLeaderIds()))
			{
				criteria.add(Restrictions.sqlRestriction(
						"FIND_IN_SET(?, `group_leader_ids`) > 0", domain.getGroupLeaderIds(), Hibernate.STRING));
			}
			else if (UtilString.isNotEmpty(domain.getGroupMemberIds()))
			{
				criteria.add(Restrictions.sqlRestriction(
						"FIND_IN_SET(?, `group_members_ids`) > 0", domain.getGroupMemberIds(), Hibernate.STRING));
			}
			
			if (UtilString.isNotEmpty(domain.getGroupLeaders(), domain.getGroupMembers()))
			{
				criteria.add(Restrictions.and(
					Restrictions.like("groupLeaders", domain.getGroupLeaders(), MatchMode.ANYWHERE),
					Restrictions.like("groupMembers", domain.getGroupMembers(), MatchMode.ANYWHERE)));
			}
			else if (UtilString.isNotEmpty(domain.getGroupLeaders()))
			{
				criteria.add(Restrictions.like("groupLeaders", domain.getGroupLeaders(), MatchMode.ANYWHERE));
			}
			else if (UtilString.isNotEmpty(domain.getGroupMembers()))
			{
				criteria.add(Restrictions.like("groupMembers", domain.getGroupMembers(), MatchMode.ANYWHERE));
			}
		}
		
		criteria.addOrder(Order.desc("groupUpdateTime"));
		
		return criteria;
	}
	
	public DAOAppProjectGroup getDaoAppProjectGroup()
	{
		return daoAppProjectGroup;
	}

	public void setDaoAppProjectGroup(DAOAppProjectGroup daoAppProjectGroup)
	{
		this.daoAppProjectGroup = daoAppProjectGroup;
	}

}
